//
// Created by 王乐 on 2022/2/9.
//

#include "LeParticleBehaviour.h"
#include "LeParticleGameObject.h"
#include "LeTime.h"
#include "LeParticleSystem.h"
#include "LeUtils.h"

LeParticleBehaviour::LeParticleBehaviour()
{

}

LeParticleBehaviour::~LeParticleBehaviour()
{

}

void LeParticleBehaviour::Start()
{
    m_pParticleGameObject = dynamic_cast<LeParticleGameObject*>(m_pGameObject);
    m_pParticle = &m_pParticleGameObject->m_Particle;
    m_CurLife = 0;
    m_AlphaValue = 1;
    m_RValue = m_pParticle->m_RValue;
}

void LeParticleBehaviour::Update()
{
    if(m_pParticleGameObject->isActive())
    {
        m_CurLife += LeTime::dDeltaTime;
        m_pTransform->m_Position += m_pParticle->m_Velocity * LeTime::dDeltaTime;
        m_pTransform->m_Rotation += glm::vec3(0, 0, m_pParticle->m_AngularSpeed * LeTime::dDeltaTime);
        m_pTransform->m_Scale += m_pParticle->m_ScaleChangeValue * LeTime::dDeltaTime;

        m_AlphaValue = LeMath::lerpFloat(m_AlphaValue, 0, 0.03);

        if(m_CurLife > m_pParticle->m_LiftTime)
        {
            m_CurLife = 0;
            m_AlphaValue = 1;
            m_pTransform->m_Position = m_pParticle->m_StartPosition;
            m_pTransform->m_Rotation = glm::vec3(0, 0, m_pParticle->m_StartRotation);
            m_pTransform->m_Scale = m_pParticle->m_StartScale;
            if(m_pParticle->m_Name == std::string("MyDust"))
                m_pParticleGameObject->getParticleSystem()->returnParticleGameObject(m_pParticleGameObject);
//                int ForFireDisplay = 0;
            else if(m_pParticle->m_Name == std::string("MyShadow"))
                m_pParticleGameObject->getParticleSystem()->returnShadowGameObject(m_pParticleGameObject);
        }
    }
}